
package com.afd.ozblip.util;

import java.util.HashMap;
import java.util.Map;

import com.afd.ozblip.entity.NoticeSetEntity;
import com.afd.ozblip.entity.UserEntity;

import android.content.Context;
import android.content.SharedPreferences;

public class SharedUtil {

    public static String fileName = "OzBlip_";

    /**
     * 存储登录相关值
     * 
     * @param activity
     * @param oauth
     */
    public static boolean storeLoginInfo(Context context, String emailStr, String pwdStr,
            UserEntity userInfo) {
        try {
            SharedPreferences settings = context.getSharedPreferences(fileName
                    + "login", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("userEmail", emailStr);
            editor.putString("userPwd", pwdStr);
            if (userInfo != null) {
                editor.putString("userId", userInfo.getUserId());
                editor.putString("headUrl", userInfo.getUserHeadUrl());
                editor.putString("nickName", userInfo.getUserNickName());
                editor.putString("level", userInfo.getUserLevel());
                editor.putString("integral", userInfo.getUserIntegral());
                editor.putString("signature", userInfo.getUserSignature());
                editor.putString("sex", userInfo.getUserSex());
                editor.putString("age", userInfo.getUserAge());
                editor.putString("career", userInfo.getUserCareer());
                editor.putString("telephone", userInfo.getUserTelephone());
            }
            editor.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 清楚用户信息
     * 
     * @return
     */
    public static void clearUserInfo(Context context) {
        try {
            SharedPreferences settings = context.getSharedPreferences(fileName
                    + "login", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.remove("userId");
            editor.remove("headUrl");
            editor.remove("nickNames");
            editor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取登录相关值
     * 
     * @return
     */
    public static String fetchLoginInfo(Context context) {
        SharedPreferences settings = context.getSharedPreferences(fileName
                + "login", Context.MODE_PRIVATE);
        String emailStr = "";
        if (settings != null) {
            emailStr = settings.getString("userEmail", "");
        }
        return emailStr;
    }

    /**
     * 获取登录相关值
     * 
     * @return
     */
    public static String[] fetchLoginInfo2(Context context) {
        SharedPreferences settings = context.getSharedPreferences(fileName
                + "login", Context.MODE_PRIVATE);
        String[] result = new String[2];
        if (settings != null) {
            String emailStr = settings.getString("userEmail", "");
            String pwdStr = settings.getString("userPwd", "");
            result[0] = emailStr;
            result[1] = pwdStr;
        }
        return result;
    }

    /**
     * 获取用户相关信息
     * 
     * @param activity
     * @param weiboType
     * @return
     */
    public static UserEntity fetchUserInfo(Context context) {
        SharedPreferences settings = context.getSharedPreferences(fileName
                + "login", Context.MODE_PRIVATE);
        UserEntity userInfo = null;
        try {
            if (settings != null) {
                String userId = settings.getString("userId", "");
                if (!StringUtil.isNullOrEmpty(userId)) {
                    userInfo = new UserEntity();
                    userInfo.setUserId(userId);
                    userInfo.setUserHeadUrl(settings.getString("headUrl", ""));
                    userInfo.setUserNickName(settings.getString("nickName", ""));
                    userInfo.setUserLevel(settings.getString("level", ""));
                    userInfo.setUserIntegral(settings.getString("integral", ""));
                    userInfo.setUserSignature(settings.getString("signature",
                            ""));
                    userInfo.setUserSex(settings.getString("sex", ""));
                    userInfo.setUserAge(settings.getString("age", ""));
                    userInfo.setUserCareer(settings.getString("career", ""));
                    userInfo.setUserTelephone(settings.getString("telephone",
                            ""));
                }
            }
        } catch (Exception e) {
        }

        return userInfo;
    }

    /**
     * 存储消息通知相关值
     * 
     * @param activity
     * @param oauth
     */
    public static boolean storeNoticeInfo(Context context, NoticeSetEntity nse) {
        try {
            SharedPreferences settings = context.getSharedPreferences(fileName
                    + "notice", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("activityFlag", nse.isActivityFlag());
            editor.putBoolean("noticeFlag", nse.isNoticeFlag());
            editor.putBoolean("updateFlag", nse.isUpdateFlag());
            editor.putBoolean("messageFlag", nse.isMessageFlag());
            editor.putString("noticeBeginTime", nse.getBeginTime());
            editor.putString("noticeEndTime", nse.getEndTime());
            editor.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取通知相关信息
     * 
     * @param activity
     * @param weiboType
     * @return
     */
    public static NoticeSetEntity fetchNoticeInfo(Context context) {
        SharedPreferences settings = context.getSharedPreferences(fileName
                + "notice", Context.MODE_PRIVATE);
        NoticeSetEntity noticeInfo = null;
        try {
            if (settings != null) {
                noticeInfo = new NoticeSetEntity();
                boolean activityFlag = settings.getBoolean("activityFlag",
                        false);
                noticeInfo.setActivityFlag(activityFlag);
                boolean noticeFlag = settings.getBoolean("noticeFlag", false);
                noticeInfo.setNoticeFlag(noticeFlag);
                boolean updateFlag = settings.getBoolean("updateFlag", false);
                noticeInfo.setUpdateFlag(updateFlag);
                boolean messageFlag = settings.getBoolean("messageFlag", false);
                noticeInfo.setMessageFlag(messageFlag);
                noticeInfo.setBeginTime(settings.getString("noticeBeginTime",
                        "10:00"));
                noticeInfo.setEndTime(settings.getString("noticeEndTime",
                        "23:00"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            noticeInfo = null;
        }

        return noticeInfo;
    }

    /**
     * 存储语言设置值
     * 
     * @param activity
     * @param oauth
     */
    public static boolean storeLanguageInfo(Context context,
            String languageLocale) {
        try {
            SharedPreferences settings = context.getSharedPreferences(fileName
                    + "language", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("languageLocale", languageLocale);
            editor.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取语言相关值
     * 
     * @param activity
     * @param weiboType
     * @return
     */
    public static String fetchLanguageInfo(Context context) {
        SharedPreferences settings = context.getSharedPreferences(fileName
                + "language", Context.MODE_PRIVATE);
        String language = "";
        if (settings != null) {
            language = settings.getString("languageLocale", "zh");
        } else {
            language = "zh";
        }
        return language;
    }

    /**
     * 存储隐私设置相关值
     * 
     * @param activity
     * @param oauth
     */
    public static boolean storePrivacyInfo(Context context, int verifyVal) {
        try {
            SharedPreferences settings = context.getSharedPreferences(fileName
                    + "privacy", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.putInt("verifyVal", verifyVal);
            editor.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 存储隐私设置相关值
     * 
     * @param activity
     * @param oauth
     */
    public static boolean storePrivacyInfo(Context context, boolean nearbyAllow) {
        try {
            SharedPreferences settings = context.getSharedPreferences(fileName
                    + "privacy", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("nearbyAllow", nearbyAllow);
            editor.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取隐私相关信息
     * 
     * @param activity
     * @param weiboType
     * @return
     */
    public static Map<String, Object> fetchPrivacyInfo(Context context) {
        SharedPreferences settings = context.getSharedPreferences(fileName
                + "privacy", Context.MODE_PRIVATE);
        Map<String, Object> privacyInfo = null;
        try {
            if (settings != null) {
                privacyInfo = new HashMap<String, Object>();
                int verifyVal = settings.getInt("verifyVal", 0);
                privacyInfo.put("verifyVal", verifyVal);
                boolean nearbyAllow = settings.getBoolean("nearbyAllow", false);
                privacyInfo.put("nearbyAllow", nearbyAllow);
            }
        } catch (Exception e) {
            e.printStackTrace();
            privacyInfo = null;
        }

        return privacyInfo;
    }

}
